package kr.ac.ssu.yoobh17.mPlayer.list_play;

import kr.ac.ssu.yoobh17.mPlayer.GlobalClass;
import kr.ac.ssu.yoobh17.mPlayer.R;
import kr.ac.ssu.yoobh17.mPlayer.data.PlayListData;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;


public class PlayListFragment extends SherlockFragment implements OnItemClickListener, OnItemLongClickListener {

	private FrameLayout	layout;

	private Cursor					playListCursor			= null;
	private PlayListCursorAdapter	playListCursorAdapter	= null;
	private ListView				listView				= null;

	@Override
	public void onAttach( Activity activity ) {

		// TODO Auto-generated method stub
		super.onAttach( activity );
		
		setListAdapter();
		
	}

	private void setListAdapter() {

		// TODO Auto-generated method stub
		
		Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
		
		String[] 	projection 		= {
										MediaStore.Audio.Playlists._ID,
										MediaStore.Audio.Playlists.NAME
							  		  };
		String 		selection 		= null;
		String[] 	selectionArgs 	= null;
		String 		sortOrder 		= "name ASC";
		
		playListCursor = getSherlockActivity().getContentResolver()
				.query( uri, projection, selection, selectionArgs, sortOrder );
		
		playListCursorAdapter = new PlayListCursorAdapter( getSherlockActivity().getApplicationContext(), playListCursor );

	}

	@Override
	public void onCreate( Bundle savedInstanceState ) {

		// TODO Auto-generated method stub
		super.onCreate( savedInstanceState );
		
	}

	@Override
	public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {

		// TODO Auto-generated method stub
		
		layout = (FrameLayout) inflater.inflate( R.layout.fragment_container_play_list, container, false );
		
		setListView();
		
		return layout;
	}

	private void setListView() {

		// TODO Auto-generated method stub
		
		listView = (ListView) layout.findViewById( R.id.listView_playList );
		
		listView.setAdapter( playListCursorAdapter );
		listView.setOnItemClickListener( this );
		listView.setOnItemLongClickListener( this );
		
	}

	@Override
	public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {

		// TODO Auto-generated method stub

		playListCursor.moveToPosition( position );


		PlayListData playListData = new PlayListData();
		
		playListData.setId	( playListCursor.getLong( 0 ) );
		playListData.setPlayListTitle( playListCursor.getString( 1 ) );
		
		Bundle argument = new Bundle();
		argument.putParcelable( "ArtistData", playListData );
		
		SelectedPlayListFragment selectedFragment = new SelectedPlayListFragment();
		selectedFragment.setArguments( argument );
		
		FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction();

		final int enter 	= R.anim.anim_fade_in;
		final int exit 		= R.anim.anim_fade_out;
		final int popEnter	= R.anim.anim_fade_in;
		final int popExit	= R.anim.anim_fade_out;

		transaction.setCustomAnimations( enter, exit, popEnter, popExit );
		
		transaction.replace( R.id.FrameLayout_musicList, selectedFragment );
		transaction.addToBackStack( null );
		transaction.commit();
		
	}

	@Override
	public boolean onItemLongClick( AdapterView<?> parent, View view, int position, long id ) {

		// TODO Auto-generated method stub

		playListCursor.moveToPosition( position );
		
		String string = String.format( "Position: %d / ID: %d", position, id );

		GlobalClass.getToastMessage( getSherlockActivity(), string );
		
		return true;
	}

	@Override
	public void onPause() {

		// TODO Auto-generated method stub
		
		super.onPause();
	}

	@Override
	public void onDestroy() {

		// TODO Auto-generated method stub

		if( null != playListCursor ){
			
			playListCursor.close();
			
		}
		
		super.onDestroy();
	}

	@Override
	public void onDetach() {

		// TODO Auto-generated method stub
		
		super.onDetach();
	}

}











